package com.puman.watchtrade.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;

import android.util.Log;


public class HttpClent {
	private static SimpleDateFormat LogSdf = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");// ��־�������ʽ

	public static String getUrlInfo(String inputUrl) {
		
		String line = null;
		String strBuf = "";
		try {
			URL url = new URL(inputUrl);
			if (url != null) {
				HttpURLConnection httpUrlConn = (HttpURLConnection) url
						.openConnection();
				httpUrlConn
						.setRequestProperty("User-Agent",
								"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; YINUOINFO API; YN_PROJECT)");

				httpUrlConn.setRequestMethod("GET");
				httpUrlConn.setDoInput(true);
				httpUrlConn.setConnectTimeout(10000);
				httpUrlConn.setReadTimeout(10000);
				int resCode = httpUrlConn.getResponseCode();
				httpUrlConn.connect();
				InputStreamReader inRead = new InputStreamReader(
						httpUrlConn.getInputStream());
				BufferedReader bufRead = new BufferedReader(inRead);
				while ((line = bufRead.readLine()) != null) {
					strBuf += line;
				}
				httpUrlConn.disconnect();

				bufRead.close();
			}
		} catch (Exception e) { // Report any errors that arise
			System.err.println(e);
			System.err.println("Usage: HttpClient:   <URL>   [<filename>]");
			strBuf += "Usage: HttpClient" + inputUrl + " [<filename>]";
		}

//		LogHelper.i("http_deg.", inputUrl + "\n  result:" + strBuf);

		Log.i("http_deg.", inputUrl + "\n  result:" + strBuf);
		// �����ʼ�
		// Date nowtime = new Date();
		// String nowTimeStr = LogSdf.format(nowtime);
		// String maeesge = "url:" + inputUrl +"\n  result:" + strBuf;
		// LogHelper logHelper = new LogHelper();
		// logHelper.sendMail(nowTimeStr + maeesge, qqMail, password);
		return strBuf;
	}

	/**
	 * ��ָ�� URL ����POST����������
	 * 
	 * @param url
	 *            ��������� URL
	 * @param param
	 *            ��������������Ӧ���� name1=value1&name2=value2 ����ʽ��
	 * @return ����Զ����Դ����Ӧ���
	 */
	public static String getUrlInfo(String url, String param) {
		PrintWriter out = null;
		BufferedReader in = null;
		String result = "";
		
		Log.i("curry==",url+param);
		
		try {
			URL realUrl = new URL(url);
			// �򿪺�URL֮�������
			URLConnection conn = realUrl.openConnection();
			// ����ͨ�õ���������
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("User-Agent",
					"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; YINUOINFO API; YN_PROJECT)");

			// ����POST�������������������
			conn.setDoOutput(true);
			conn.setDoInput(true);
			// ��ȡURLConnection�����Ӧ�������
			out = new PrintWriter(conn.getOutputStream());
			// �����������
			out.print(param);
			// flush������Ļ���
			out.flush();
			// ����BufferedReader����������ȡURL����Ӧ
			in = new BufferedReader(
					new InputStreamReader(conn.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
		} catch (Exception e) {
			result += "���� POST ��������쳣��" + e;
			e.printStackTrace();
		}
		// ʹ��finally�����ر��������������
		finally {
			try {
				if (out != null) {
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}

		Log.i("http_deg.", url + param + "\n  result:" + result);
		return result;
	}

}